import { Cookie } from 'electron';
import { defineStore } from 'pinia'

export const useCookieStore = defineStore('cookie', {
  state: () => ({
    mappings: {}
  }),

  actions: {
    push(cookie: Cookie) {
      const domain = cookie.domain;
      if (!domain) {
        return;
      }
      const cookies = this.mappings[domain] || [];
      const item = cookies.filter(c => c.name == cookie.name)[0];
      if (item) {
        for (const key in cookie) {
          item[key] = cookie[key]
        }

      }
      else {
        cookies.push(cookie);
      }
      this.mappings[domain] = cookies;

    },
    get(domain: string) {
      if (domain.startsWith('http://')) {
        domain = domain.substring(7);
      }
      else if (domain.startsWith('https://')) {
        domain = domain.substring(8);
      }
      return this.mappings[domain] || []
    }



  }

})

